package com.sailfish.springbootdemo.service.db7.Impl;

import com.alibaba.fastjson.JSONObject;
import com.sailfish.springbootdemo.configs.UserHeaderHolder;
import com.sailfish.springbootdemo.dao.db4.User2Dao;
import com.sailfish.springbootdemo.dao.db7.TbFlowProcessDetailDao;
import com.sailfish.springbootdemo.dao.db7.TbFlowProcessMainDao;
import com.sailfish.springbootdemo.dao.db7.TbFlowProcessRecordsDao;
import com.sailfish.springbootdemo.dao.db7.TbTemplateVersionDao;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db7.TbFlowProcessDetail;
import com.sailfish.springbootdemo.pojo.db7.TbFlowProcessMain;
import com.sailfish.springbootdemo.pojo.db7.TbFlowProcessRecords;
import com.sailfish.springbootdemo.pojo.db7.TbTemplateVersion;
import com.sailfish.springbootdemo.service.db7.TbFlowProcessMainService;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class TbFlowProcessMainServiceImpl implements  TbFlowProcessMainService{

    @Autowired
    private TbFlowProcessMainDao tbFlowProcessMainDao;

    @Autowired
    private TbFlowProcessDetailDao tbFlowProcessDetailDao;

    @Autowired
    private TbFlowProcessRecordsDao tbFlowProcessRecordsDao;

    @Autowired
    private TbTemplateVersionDao tbTemplateVersionDao;

    @Autowired
    private User2Dao user2Dao;

    @Override
    public Result getAllTask(String data) {
        try {
            //解析Json字符串
            //判断是否为空
            if (data != null && !data.isEmpty()) {
                TbFlowProcessMain param = new TbFlowProcessMain();

                JSONObject jsonObject = JSONObject.parseObject(data);
                String _flow_type = jsonObject.getString("flow_type");

                String _status = jsonObject.getString("status");

                if (_status != null && !_status.isEmpty()) {
                    try {
                        Integer temp=Integer.parseInt(_status);
                        param.setStatus(temp);
                    } catch (NumberFormatException e) {
                        return ResultUtil.error(500, "status 格式错误", "", null);
                    }
                }

                if (_flow_type != null && !_flow_type.isEmpty()) {
                    param.setFlow_type(_flow_type);
                }

                List<TbFlowProcessMain> tbFlowProcessMains = tbFlowProcessMainDao.getFlowProcessMain(param);

                if (tbFlowProcessMains != null && tbFlowProcessMains.size() > 0) {
                    for (TbFlowProcessMain detail : tbFlowProcessMains) {
                        String create = detail.getCreate_user();
                        String nowApprove= detail.getNow_approver();
                        String flowNo= detail.getFlow_no();

                        if (create != null && !create.isEmpty()) {
                            List<String> list = Arrays.asList(create.split(","));

                            detail.setCreate_user_info(user2Dao.getUserInfoByIds(list));
                        }

                        if (nowApprove != null && !nowApprove.isEmpty()) {
                            List<String> list = Arrays.asList(nowApprove.split(","));
                            detail.setNow_approver_info(user2Dao.getUserInfoByIds(list));
                        }

                        if (flowNo != null && !flowNo.isEmpty()) {
                            TbTemplateVersion templateVersion=new TbTemplateVersion();
                            templateVersion.setFlow_no(flowNo);

                            detail.setTemplate_version_info(tbTemplateVersionDao.getTemplateVersionList(templateVersion));
                        }

                    }
                }

                return  ResultUtil.success(tbFlowProcessMains);
            }
            else
            {
                return ResultUtil.error(500, "参数为空", "", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), "", null);
        }
    }

    @Override
    public Result getMyTask(String data) {
        try {
            //解析Json字符串
            //判断是否为空
            if (data != null && !data.isEmpty()) {
                TbFlowProcessMain param = new TbFlowProcessMain();

                JSONObject jsonObject = JSONObject.parseObject(data);
                String _flow_type = jsonObject.getString("flow_type");

                String _status = jsonObject.getString("status");


                if (_status != null && !_status.isEmpty()) {
                    try {
                        Integer temp=Integer.parseInt(_status);
                        param.setStatus(temp);
                    } catch (NumberFormatException e) {
                        return ResultUtil.error(500, "status 格式错误", "", null);
                    }
                }

                if (_flow_type != null && !_flow_type.isEmpty()) {
                    param.setFlow_type(_flow_type);
                }

                param.setCreate_user(UserHeaderHolder.getUserId());
                param.setNow_approver(UserHeaderHolder.getUserId());

                List<TbFlowProcessMain> tbFlowProcessMains = tbFlowProcessMainDao.getFlowProcessMain(param);

                if (tbFlowProcessMains != null && tbFlowProcessMains.size() > 0) {
                    for (TbFlowProcessMain detail : tbFlowProcessMains) {
                        String create = detail.getCreate_user();
                        String nowApprove= detail.getNow_approver();
                        String flowNo= detail.getFlow_no();

                        if (create != null && !create.isEmpty()) {
                            List<String> list = Arrays.asList(create.split(","));

                            detail.setCreate_user_info(user2Dao.getUserInfoByIds(list));
                        }

                        if (nowApprove != null && !nowApprove.isEmpty()) {
                            List<String> list = Arrays.asList(nowApprove.split(","));
                            detail.setNow_approver_info(user2Dao.getUserInfoByIds(list));
                        }

                        if (flowNo != null && !flowNo.isEmpty()) {
                            TbTemplateVersion templateVersion=new TbTemplateVersion();
                            templateVersion.setFlow_no(flowNo);

                            detail.setTemplate_version_info(tbTemplateVersionDao.getTemplateVersionList(templateVersion));
                        }
                    }
                }

                return  ResultUtil.success(tbFlowProcessMains);
            }
            else
            {
                return ResultUtil.error(500, "参数为空", "", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), "", null);
        }
    }


    @Override
    @Transactional(transactionManager = "MysqlTransactionManager7")
    public  Result approveFlow(String data)
    {
        try
        {
            if (data != null && !data.isEmpty())
            {
                TbFlowProcessMain param = new TbFlowProcessMain();

                JSONObject jsonObject = JSONObject.parseObject(data);
                String _flow_no = jsonObject.getString("flow_no");
                String _remark = jsonObject.getString("remark");

                if (_flow_no != null && !_flow_no.isEmpty()) {
                    param.setFlow_no(_flow_no);
                }
                else
                {
                    return ResultUtil.error(500, "flow_no 不能为空", "", null);
                }

                List<TbFlowProcessMain> tbFlowProcessMainList=tbFlowProcessMainDao.getFlowProcessMain(param);
                if (tbFlowProcessMainList != null && tbFlowProcessMainList.size() > 0)
                {
                    TbFlowProcessMain tbFlowProcessMain=tbFlowProcessMainList.get(0);

                    Integer _flow_status = tbFlowProcessMain.getStatus();
                    //1 待审核 2审核中 3审核完成
                    if (_flow_status == 3)
                    {
                        return ResultUtil.error(500, "该流程已审核完成", "", null);
                    }
                    else
                    {
                        //获取当前所在节点
                        String _flow_type = tbFlowProcessMain.getFlow_type();
                        Integer _now_level= tbFlowProcessMain.getNow_level();

                        //去查询是否有下一个节点,正常是不会为空的,为空就说明就成结束了.但是加上个非空判断吧
                        if (_now_level != null)
                        {
                            //获取流程明细去,看看有没有下一步流程,没有下一步流程就更新主表到审核完成,如果有就把下一节点,审批人更新到主表上
                            TbFlowProcessDetail param2 = new TbFlowProcessDetail();
                            param2.setFlow_no(_flow_no);
                            param2.setFlow_type(_flow_type);
                            param2.setFlow_level((_now_level+1));

                            List<TbFlowProcessDetail> tbFlowProcessDetailList=tbFlowProcessDetailDao.getFlowProcessDetail(param2);
                            if (tbFlowProcessDetailList != null && tbFlowProcessDetailList.size() > 0)
                            {
                                TbFlowProcessDetail tbFlowProcessDetail=tbFlowProcessDetailList.get(0);

                                //更新主表信息
                                TbFlowProcessMain tempFlowProcessMain=new TbFlowProcessMain();
                                tempFlowProcessMain.setFlow_no(_flow_no);
                                tempFlowProcessMain.setNow_level(tbFlowProcessDetail.getFlow_level());
                                tempFlowProcessMain.setNow_approver(tbFlowProcessDetail.getFlow_approver());
                                tempFlowProcessMain.setStatus(2);//审核中
                                tempFlowProcessMain.setUpdate_time(new Date());
                                tempFlowProcessMain.setUpdate_user(UserHeaderHolder.getUserId());
                                tbFlowProcessMainDao.updateFlowProcessMain(tempFlowProcessMain);

                                //更新明细表的信息
                                TbFlowProcessDetail tempFlowProcessDetail=new TbFlowProcessDetail();
                                tempFlowProcessDetail.setFlow_no(_flow_no);
                                tempFlowProcessDetail.setFlow_level(_now_level);
                                tempFlowProcessDetail.setStatus(2);
                                tempFlowProcessDetail.setRemark(_remark);
                                tempFlowProcessDetail.setUpdate_time(new Date());
                                tempFlowProcessDetail.setUpdate_user(UserHeaderHolder.getUserId());
                                tbFlowProcessDetailDao.updateFlowProcessDetail(tempFlowProcessDetail);

                                //写入审核记录表
                                TbFlowProcessDetail param3 = new TbFlowProcessDetail();
                                param3.setFlow_no(_flow_no);
                                param3.setFlow_type(_flow_type);
                                param3.setFlow_level(_now_level);
                                List<TbFlowProcessDetail> tbFlowProcessDetailList2=tbFlowProcessDetailDao.getFlowProcessDetail(param3);
                                TbFlowProcessDetail temp=tbFlowProcessDetailList2.get(0);

                                TbFlowProcessRecords tbFlowProcessRecords=new TbFlowProcessRecords();
                                tbFlowProcessRecords.setFlow_no(_flow_no);
                                tbFlowProcessRecords.setFlow_type(_flow_type);
                                tbFlowProcessRecords.setFlow_name(temp.getFlow_name());
                                tbFlowProcessRecords.setFlow_level(_now_level);
                                tbFlowProcessRecords.setFlow_approver(tbFlowProcessMain.getNow_approver());
                                tbFlowProcessRecords.setResult("审核通过");
                                tbFlowProcessRecords.setRemark(_remark);
                                tbFlowProcessRecords.setCreate_time(new Date());
                                tbFlowProcessRecords.setCreate_user(UserHeaderHolder.getUserId());

                                tbFlowProcessRecordsDao.insertFlowProcessRecords(tbFlowProcessRecords);
                            }
                            else
                            {
                                //更新主表信息
                                TbFlowProcessMain tempFlowProcessMain=new TbFlowProcessMain();
                                tempFlowProcessMain.setFlow_no(_flow_no);
                                tempFlowProcessMain.setNow_level(0);
                                tempFlowProcessMain.setNow_approver("");
                                tempFlowProcessMain.setStatus(3);
                                tempFlowProcessMain.setUpdate_time(new Date());
                                tempFlowProcessMain.setUpdate_user(UserHeaderHolder.getUserId());
                                tbFlowProcessMainDao.updateFlowProcessMain(tempFlowProcessMain);

                                //更新明细表的信息
                                TbFlowProcessDetail tempFlowProcessDetail=new TbFlowProcessDetail();
                                tempFlowProcessDetail.setFlow_no(_flow_no);
                                tempFlowProcessDetail.setFlow_level(_now_level);
                                tempFlowProcessDetail.setStatus(2);
                                tempFlowProcessDetail.setRemark(_remark);
                                tempFlowProcessDetail.setUpdate_time(new Date());
                                tempFlowProcessDetail.setUpdate_user(UserHeaderHolder.getUserId());
                                tbFlowProcessDetailDao.updateFlowProcessDetail(tempFlowProcessDetail);

                                //写入审核记录表
                                TbFlowProcessDetail param3 = new TbFlowProcessDetail();
                                param3.setFlow_no(_flow_no);
                                param3.setFlow_type(_flow_type);
                                param3.setFlow_level(_now_level);
                                List<TbFlowProcessDetail> tbFlowProcessDetailList2=tbFlowProcessDetailDao.getFlowProcessDetail(param3);
                                TbFlowProcessDetail temp=tbFlowProcessDetailList2.get(0);

                                TbFlowProcessRecords tbFlowProcessRecords=new TbFlowProcessRecords();
                                tbFlowProcessRecords.setFlow_no(_flow_no);
                                tbFlowProcessRecords.setFlow_type(_flow_type);
                                tbFlowProcessRecords.setFlow_name(temp.getFlow_name());
                                tbFlowProcessRecords.setFlow_level(_now_level);
                                tbFlowProcessRecords.setFlow_approver(tbFlowProcessMain.getNow_approver());
                                tbFlowProcessRecords.setResult("审核完成");
                                tbFlowProcessRecords.setRemark(_remark);
                                tbFlowProcessRecords.setCreate_time(new Date());
                                tbFlowProcessRecords.setCreate_user(UserHeaderHolder.getUserId());

                                tbFlowProcessRecordsDao.insertFlowProcessRecords(tbFlowProcessRecords);

                                //更新版本表
                                TbTemplateVersion templateVersion=new TbTemplateVersion();
                                templateVersion.setFlow_no(_flow_no);
                                templateVersion.setStatus(2);
                                tbTemplateVersionDao.updateTemplateVersionStatus(templateVersion);

                            }

                            return ResultUtil.success("审核成功");
                        }
                        else
                        {
                            return ResultUtil.error(500, "该流程已审核完成", "", null);
                        }
                    }
                }
                else
                {
                    return ResultUtil.error(500, "未查询到该流程", "", null);
                }
            }
            else
            {
                return ResultUtil.error(500, "参数为空", "", null);
            }


        }
        catch (Exception e)
        {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), "", null);
        }
    }

    @Override
    public  Result getFlowProcessRecords(String data)
    {
        try {
            //解析Json字符串
            //判断是否为空
            if (data != null && !data.isEmpty()) {
                TbFlowProcessRecords param = new TbFlowProcessRecords();

                JSONObject jsonObject = JSONObject.parseObject(data);
                String _flow_no = jsonObject.getString("flow_no");

                if (_flow_no != null && !_flow_no.isEmpty()) {
                    param.setFlow_no(_flow_no);
                }

                List<TbFlowProcessRecords> tbFlowProcessRecordsList = tbFlowProcessRecordsDao.getTbFlowProcessRecordsList(param);

                if (tbFlowProcessRecordsList != null && tbFlowProcessRecordsList.size() > 0) {
                    for (TbFlowProcessRecords detail : tbFlowProcessRecordsList) {
                        String flowApprove= detail.getFlow_approver();

                        if (flowApprove != null && !flowApprove.isEmpty()) {
                            List<String> list = Arrays.asList(flowApprove.split(","));
                            detail.setFlow_approver_info(user2Dao.getUserInfoByIds(list));
                        }
                    }
                }

                return  ResultUtil.success(tbFlowProcessRecordsList);
            }
            else
            {
                return ResultUtil.error(500, "参数为空", "", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), "", null);
        }
    }

}
